package com.dodock.banglapapers.server.helper;

import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.google.appengine.api.search.ExpressionParser.cmpExpr_return;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;

public class DDNNewsListIttefaq implements DDNNewsListInterface {

	private String BASE_URL = "http://www.ittefaq.com.bd/index.php";
	private String CATEGORY_BASE = "http://www.ittefaq.com.bd/index.php?ref=";
	
	@Override
	public String GenerateNewsList(String categoryid) {
		try {
			String url = "";
			if (categoryid.equalsIgnoreCase("lead")) {
				url = BASE_URL;
			} 
			
			if (categoryid.equalsIgnoreCase("first_page")) {
				url = CATEGORY_BASE + getEncodedRef("20","1_1");//20_07_26_13_1_1
			}
			
			if (categoryid.equalsIgnoreCase("last_page")) {
				url = CATEGORY_BASE + getEncodedRef("20","1_2");//20_07_26_13_1_2
			}
			
			if (categoryid.equalsIgnoreCase("capital")) {
				url = CATEGORY_BASE + getEncodedRef("20","1_6");//20_07_26_13_1_6
			}
			
			if (categoryid.equalsIgnoreCase("world")) {
				url = CATEGORY_BASE + getEncodedRef("20","1_7");//20_07_26_13_1_7
			}
			
			if (categoryid.equalsIgnoreCase("entertainment")) {
				url = CATEGORY_BASE + getEncodedRef("20","1_9");//20_07_26_13_1_9
			}
			
			if (categoryid.equalsIgnoreCase("business")) {
				url = CATEGORY_BASE + getEncodedRef("20","1_11");//20_07_26_13_1_11
			}
			
			if (categoryid.equalsIgnoreCase("sports")) {
				url = CATEGORY_BASE + getEncodedRef("20","1_8");//20_07_26_13_1_8
			}
			
			if (categoryid.equalsIgnoreCase("country")) {
				url = CATEGORY_BASE + getEncodedRef("20","1_25");//20_07_26_13_1_25
			}
			
			if (categoryid.equalsIgnoreCase("other")) {
				url = CATEGORY_BASE + getEncodedRef("20","1_12");//20_07_26_13_1_12
			}
			
			if (categoryid.equalsIgnoreCase("tech")) {
				url = CATEGORY_BASE + getEncodedRef("20","1_33");//20_07_26_13_1_33
			}
			
			return getNewsList(url, categoryid);
		} catch (Exception ex) {
			System.out.print("exeception : " + ex.toString());
			return "[]";
		}
	}

	private String getEncodedRef(String prefix, String postfix){
		java.util.TimeZone tz = java.util.TimeZone.getTimeZone("GMT+6");
		java.util.Calendar c = java.util.Calendar.getInstance(tz);
		String candidate = String.format("%s_%02d_%02d_%02d_%s",
				prefix,
				c.get(java.util.Calendar.MONTH)+1,
				c.get(java.util.Calendar.DAY_OF_MONTH),
				c.get(java.util.Calendar.YEAR)%2000,
				postfix);
		return org.apache.commons.codec.binary.Base64.encodeBase64String(candidate.getBytes());
	}
	
	
	private String getNewsList(String url, String categoryid) {
		JsonArray array = new JsonArray();
		Document doc;
		try {
			Connection.Response response = Jsoup
					.connect(url)
					.userAgent(
							"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.21 (KHTML, like Gecko) Chrome/19.0.1042.0 Safari/535.21")
					.timeout(10000).execute();
			doc = response.parse();
			
			if (categoryid.equalsIgnoreCase("lead")) {
				generateLeadNewsList(doc, array);
			} else {
				generateCategoryNewsList(doc, array);
			}
			return array.toString();
		} catch (Exception e) {
			System.out.println(e.toString());
			return "[]";
		}
	}
	
	private void generateCategoryNewsList(Document doc, JsonArray array) {
		Elements newsElements = doc.select("div[class=menuNewsImg_MenuNews1]");
		for(Element newsElement : newsElements){
			parseElement(newsElement, array);
		}
		
		Elements extraNewsElements = doc.select("tr[class=menuNewsHeadline]");
		for(Element extraNews : extraNewsElements){
			parseExtraNews(extraNews, array);
		}
	}

	
	
	private void generateLeadNewsList(Document doc, JsonArray array) {
		Element titleNews = doc.select("div[class=menuNewsImg_Lead]").first();
		if(titleNews != null){
			parseElement(titleNews, array);
		}
		Elements boxNewsElements = doc.select("div[class=menuNewsPanel_top3]");
		for(Element boxNewElement : boxNewsElements){
			parseElement(boxNewElement, array);
		}
		Elements slidingNewsElements = doc.select("div[class=menuNewsHeadlineImg_SideNews]");
		for(Element slidingNewsElement : slidingNewsElements){
			parseElement(slidingNewsElement, array);
		}
	}
	
	private void parseElement(Element element, JsonArray array){
		String id = element.select("a").first().attr("href");
		String image = element.select("img").first().attr("src");
		String title = element.select("img").first().attr("alt");

		JsonObject obj = new JsonObject();
		obj.addProperty("id", id);
		obj.addProperty("title", title);
		obj.addProperty("image", Settings.WWW_ITTEFAQ_COM + image);
		array.add(obj);
	}

	private void parseExtraNews(Element element, JsonArray array){
		String id = element.select("a").first().attr("href");
		String title = element.select("a").first().text();
		JsonObject obj = new JsonObject();
		obj.addProperty("id", id);
		obj.addProperty("title", title);
		array.add(obj);
	}
}
